// import { app } from "../../app";
// import { CameraComp } from "../utils/CameraComp";

const { ccclass, property, requireComponent, executeInEditMode, menu } = cc._decorator;

@ccclass
@executeInEditMode
@menu("工具/自定义点击")
export default class HookHit extends cc.Component {
    polygonCollider: cc.PolygonCollider = null;

    onLoad() {
        // 添加多边形组件
        this.polygonCollider = this.getComponent(cc.PolygonCollider);
        if (!this.polygonCollider) {
            this.polygonCollider = this.addComponent(cc.PolygonCollider);
            this.polygonCollider['threshold'] = 4;
        };
        this.node["_hitTest"] = this.hitTest.bind(this);
    };

    // 穿透检测
    hitTest(point: cc.Vec2, listener) {
        if (!this.node.activeInHierarchy) { return false; };

        let worldPos = cc.v2();
        // CameraComp.ins.camera.getScreenToWorldPoint(point, worldPos);
        let nodePos = this.node.convertToNodeSpaceAR(worldPos);
        nodePos.subSelf(this.polygonCollider.offset);
        return cc.Intersection.pointInPolygon(nodePos, this.polygonCollider.points);
        // let worldPos = point.addSelf(CameraComp.ins.node.getPosition());
        // let nodePos = this.node.convertToNodeSpaceAR(worldPos);
        // nodePos.subSelf(this.polygonCollider.offset);
        // return cc.Intersection.pointInPolygon(nodePos, this.polygonCollider.points);
    }
}